package com.goldplusgold.tradedelay.app.trade.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.adapter.AdapterParentBase;
import com.goldplusgold.support.lib.widget.horizontallistview.SyncHorizontalScrollView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.trade.dialog.CancelOrderDialog;
import com.goldplusgold.tradedelay.app.trade.fragment.TradeFragment;
import com.goldplusgold.tradedelay.app.trade.pojo.EntrustOrder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/5/18.
 */

public class AdapterEntrust extends AdapterParentBase<EntrustOrder> implements AdapterView.OnItemClickListener, SyncHorizontalScrollView.OnScrollListenter {
    private ArrayList<SyncHorizontalScrollView> mScrollViewList = new ArrayList<>();


    public AdapterEntrust(Context ctx) {
        super(ctx);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.item_entrust, parent, false);
            holder = new ViewHolder(convertView);
            for (int i = 0; i < 6; i++) {
                View childView = getLayoutInflater().inflate(R.layout.item_horizontal_position, holder.mListViewPosition, false);
                holder.mListViewPosition.addView(childView);
            }
            mScrollViewList.add(holder.mSyncHorizontalScrollView);
            holder.mSyncHorizontalScrollView.setOnScrollListenter(this);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final EntrustOrder content = getItem(position);
        if (content != null) {
            // 合约代码
            holder.mTextViewTitlePosition.setText(content.getProdCode());
            holder.mTextViewTitlePosition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            int count = holder.mListViewPosition.getChildCount();
            for (int i = 0; i < count; i++) {
                View childView = holder.mListViewPosition.getChildAt(i);
                TextView item = (TextView) childView.findViewById(R.id.id_text_view_horizontal);
                item.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                switch (i) {
                    case 0:
                        //状态
                        item.setText(content.getEntrStat());
                        break;
                    case 1:
                        //类型
                        item.setText(content.getExchType());
                        break;
                    case 2:
                        // 委托价
                        item.setText(String.valueOf(content.getEntrPrice()));
                        break;
                    case 3:
                        //手数
                        item.setText(String.valueOf(content.getEntrAmount()));
                        break;
                    case 4:
                        // 可撤
                        int canRevokedAmount = content.getCanRevokedAmount();
                        item.setText(String.valueOf(canRevokedAmount));
                        if (canRevokedAmount > 0) {
                            holder.mCancelLinearLayout.setVisibility(View.VISIBLE);
                            holder.mCancellation.setEnabled(true);
                            holder.mCancellation.setTextColor(getContext().getResources().getColor(R.color.color_gold));
                        } else {
                            holder.mCancelLinearLayout.setVisibility(View.GONE);
                            holder.mCancellation.setEnabled(false);
                            holder.mCancellation.setTextColor(getContext().getResources().getColor(R.color.color_gray));
                        }
                        break;
                    case 5:
                        // 委托时间
                        item.setText(content.getEexchTime());
                        break;
                }

            }

            if (content.getDisplayPosition()) {
                holder.mCancelLinearLayout.setVisibility(View.VISIBLE);
            } else {
                holder.mCancelLinearLayout.setVisibility(View.GONE);
            }
            holder.mListViewPosition.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemDisplay(position);
                }
            });

            holder.mCancellation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Fragment f = getFragment();
                    if (f != null && f instanceof TradeFragment) {
                        CancelOrderDialog dialog = new CancelOrderDialog();
                        Bundle args = new Bundle();
                        args.putString("orderNo", content.getOrderNo());
                        dialog.setArguments(args);
                        dialog.show(f.getFragmentManager(), f.getTag());
                    }
                }
            });

        }
        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        position = position > 0 ? position - 1 : 0;
        itemDisplay(position);
    }


    private void itemDisplay(int position) {
        EntrustOrder content = getItem(position);
        if (content != null) {
            int count = getCount();
            for (int i = 0; i < count; i++) {
                if (position == i) {
                    if (getItem(i).getDisplayPosition()) {
                        getItem(i).setDisplayPosition(false);
                    } else {
                        getItem(i).setDisplayPosition(true);
                    }
                } else {
                    getItem(i).setDisplayPosition(false);
                }
            }
            notifyDataSetChanged();
        }
    }


    @Override
    public void onScroll(int x, int y) {
        for (SyncHorizontalScrollView scrollview :
                mScrollViewList) {
            scrollview.setScrollX(x);

        }
    }

    public void addHead(SyncHorizontalScrollView view) {
        this.mScrollViewList.add(view);
    }

    public static class ViewHolder {
        @BindView(R.id.id_text_view_title_position)
        TextView mTextViewTitlePosition;
        @BindView(R.id.id_list_view_position)
        LinearLayout mListViewPosition;
        @BindView(R.id.content_horsv)
        SyncHorizontalScrollView mSyncHorizontalScrollView;
        @BindView(R.id.id_layout_cancellation)
        LinearLayout mCancelLinearLayout;
        @BindView(R.id.id_text_view_cancellation)
        TextView mCancellation;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
